﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Transactions;

public partial class Main_Accounts_VoucherEntry_ReceiptEdit : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    int ControlCounter = 2;
    string strsql = string.Empty;
    //*****Variable for add new row*******//
    List<string> LstList;
    List<string> LstVouSerial;
    List<string> LstDrCr;
    List<string> LstLedgerName;
    List<string> LstDebit;
    List<string> LstCredit;
    List<string> LstBalance;
    //***** End Variable for add new row*******//
    List<string> LstVouSerialForDelete;
    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        _OverrideAddNewRow();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            //**************Set Minimum and maximum date calender Control********
            AccountSettings.setMinMaxDate(form1);
            //********************End **********
            LstList = new List<string>();
            ViewState["LstList"] = LstList;

            LstVouSerial = new List<string>();
            ViewState["LstVouSerial"] = LstVouSerial;

            LstDrCr = new List<string>();
            ViewState["LstDrCr"] = LstDrCr;

            LstLedgerName = new List<string>();
            ViewState["LstLedgerName"] = LstLedgerName;

            LstDebit = new List<string>();
            ViewState["LstDebit"] = LstDebit;

            LstCredit = new List<string>();
            ViewState["LstCredit"] = LstCredit;

            LstBalance = new List<string>();
            ViewState["LstBalance"] = LstBalance;

            LstVouSerialForDelete = new List<string>();
            ViewState["LstVouSerialForDelete"] = LstVouSerialForDelete;

            if (Request.QueryString["TranId"] != null)
            {
                _BindPaymentMain(Request.QueryString["TranId"].ToString());
                _BindPaymentItemDetails(Request.QueryString["TranId"].ToString());
            }
        }
    }
    private void _OverrideAddNewRow()
    {
        LstList = (List<string>)ViewState["LstList"];
        LstVouSerial = (List<string>)ViewState["LstVouSerial"];
        LstLedgerName = (List<string>)ViewState["LstLedgerName"];
        LstDrCr = (List<string>)ViewState["LstDrCr"];
        LstDebit = (List<string>)ViewState["LstDebit"];
        LstCredit = (List<string>)ViewState["LstCredit"];
        LstBalance = (List<string>)ViewState["LstBalance"];
        if (LstList != null)
        {
            int count = Convert.ToInt32(LstList.Count.ToString());
            for (int i = 0; i < count; i++)
            {
                ControlCounter++;
                HtmlTableRow htrow = new HtmlTableRow();


                HtmlTableCell htcell1 = new HtmlTableCell();
                htcell1.Width = "10%";
                htcell1.VAlign = "Top";
                DropDownList ddlDrCr = new DropDownList();
                ddlDrCr.ID = LstDrCr[i].ToString();
                ddlDrCr.Items.Add(new ListItem("Cr", "1"));
                ddlDrCr.Items.Add(new ListItem("Dr", "2"));
                ddlDrCr.SelectedIndex = 0;
                ddlDrCr.Width = Unit.Percentage(98);
                ddlDrCr.AutoPostBack = true;
                ddlDrCr.ToolTip = "Debit/Credit";
                ddlDrCr.SelectedIndexChanged += new EventHandler(ddlDrCr_SelectedIndexChanged);
                ddlDrCr.CssClass = "dropdown";
                HiddenField hidVouserial = new HiddenField();
                hidVouserial.ID = LstVouSerial[i].ToString();
                hidVouserial.Value = "0";
                htcell1.Controls.Add(ddlDrCr);
                htcell1.Controls.Add(hidVouserial);

                HtmlTableCell htcell2 = new HtmlTableCell();
                htcell2.Width = "30%";
                htcell2.VAlign = "Top";
                DropDownList ddlLedger = new DropDownList();
                ddlLedger.ID = LstLedgerName[i].ToString();
                ddlLedger.Width = Unit.Percentage(98);
                ddlLedger.AutoPostBack = true;
                ddlLedger.ToolTip = "Select Ledger for Transaction";
                ddlLedger.Attributes.Add("onchange", "return ValidateRepeatLedger(this.id);");
                ddlLedger.AutoPostBack = true;
                ddlLedger.SelectedIndexChanged += new EventHandler(ddlLedger_OnSelectedIndexChanged);
                ddlLedger.CssClass = "dropdown";
                htcell2.Controls.Add(ddlLedger);

                HtmlTableCell htcell3 = new HtmlTableCell();
                htcell3.Width = "20%";
                htcell3.VAlign = "Top";
                TextBox txtDebit = new TextBox();
                txtDebit.ID = LstDebit[i].ToString();
                txtDebit.Width = Unit.Percentage(98);
                txtDebit.ToolTip = "Debit Amount";
                txtDebit.CssClass = "inputnumber";
                txtDebit.MaxLength = 21;
                txtDebit.Enabled = false;
                txtDebit.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event);");
                txtDebit.Attributes.Add("onfocus", "return RemoveZeroOnFocusDebit(this.id);");
                txtDebit.Attributes.Add("onblur", "return SetZeroOnBlurDebit();");
                txtDebit.AutoCompleteType = AutoCompleteType.Disabled;
                htcell3.Controls.Add(txtDebit);


                HtmlTableCell htcell4 = new HtmlTableCell();
                htcell4.Width = "20%";
                htcell4.VAlign = "Top";
                TextBox txtCredit = new TextBox();
                txtCredit.ID = LstCredit[i].ToString();
                txtCredit.Width = Unit.Percentage(98);
                txtCredit.CssClass = "inputnumber";
                txtCredit.Text = "0.00";
                txtCredit.MaxLength = 21;
                txtCredit.ToolTip = "Credit Amount";
                txtCredit.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event);");
                txtCredit.Attributes.Add("onfocus", "return RemoveZeroOnFocusCredit(this.id);");
                txtCredit.Attributes.Add("onblur", "return SetZeroOnBlurCredit();");
                txtCredit.AutoCompleteType = AutoCompleteType.Disabled;
                htcell4.Controls.Add(txtCredit);

                HtmlTableCell htcell5 = new HtmlTableCell();
                htcell5.Width = "20%";
                htcell5.VAlign = "Top";
                TextBox txtBalance = new TextBox();
                txtBalance.ID = LstBalance[i].ToString();
                txtBalance.Width = Unit.Percentage(98);
                txtBalance.CssClass = "inputnumber";
                txtBalance.ToolTip = "Ledger Balance";
                txtBalance.Enabled = false;
                htcell5.Controls.Add(txtBalance);

                htrow.Cells.Add(htcell1);
                htrow.Cells.Add(htcell2);
                htrow.Cells.Add(htcell3);
                htrow.Cells.Add(htcell4);
                htrow.Cells.Add(htcell5);
                tblVouEntry.Rows.Add(htrow);

            }
        }
    }
    private void _AddNewRow()
    {
        ControlCounter++;

        HtmlTableRow htrow = new HtmlTableRow();

        HtmlTableCell htcell1 = new HtmlTableCell();
        htcell1.Width = "10%";
        htcell1.VAlign = "Top";
        DropDownList ddlDrCr = new DropDownList();
        ddlDrCr.ID = "ddlDrCr" + ControlCounter.ToString();
        ddlDrCr.Width = Unit.Percentage(98);
        ddlDrCr.Items.Add(new ListItem("Cr", "1"));
        ddlDrCr.Items.Add(new ListItem("Dr", "2"));
        ddlDrCr.SelectedIndex = 0;
        ddlDrCr.AutoPostBack = true;
        ddlDrCr.ToolTip = "Debit/Credit";
        ddlDrCr.SelectedIndexChanged += new EventHandler(ddlDrCr_SelectedIndexChanged);
        ddlDrCr.CssClass = "dropdown";
        HiddenField hidVouserial = new HiddenField();
        hidVouserial.ID = "hidVouserial" + ControlCounter.ToString();
        hidVouserial.Value = "0";
        htcell1.Controls.Add(ddlDrCr);
        htcell1.Controls.Add(hidVouserial);
        LstList.Add(ControlCounter.ToString());
        LstDrCr.Add(ddlDrCr.ID);
        LstVouSerial.Add(hidVouserial.ID);

        HtmlTableCell htcell2 = new HtmlTableCell();
        htcell2.Width = "30%";
        htcell2.VAlign = "Top";
        DropDownList ddlLedger = new DropDownList();
        ddlLedger.ID = "ddlLedger" + ControlCounter.ToString();
        ddlLedger.Width = Unit.Percentage(98);
        ddlLedger.CssClass = "dropdown";
        ddlLedger.ToolTip = "Select Ledger for Transaction";
        ddlLedger.Attributes.Add("onchange", "return ValidateRepeatLedger(this.id);");
        ddlLedger.AutoPostBack = true;
        ddlLedger.SelectedIndexChanged += new EventHandler(ddlLedger_OnSelectedIndexChanged);
        htcell2.Controls.Add(ddlLedger);
        LstLedgerName.Add(ddlLedger.ID);


        HtmlTableCell htcell3 = new HtmlTableCell();
        htcell3.Width = "20%";
        htcell3.VAlign = "Top";
        TextBox txtDebit = new TextBox();
        txtDebit.ID = "txtDebit" + ControlCounter.ToString();
        txtDebit.Width = Unit.Percentage(98);
        txtDebit.CssClass = "inputnumber";
        txtDebit.MaxLength = 21;
        txtDebit.ToolTip = "Debit Amount";
        txtDebit.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event);");
        txtDebit.Attributes.Add("onfocus", "return RemoveZeroOnFocusDebit(this.id);");
        txtDebit.Attributes.Add("onblur", "return SetZeroOnBlurDebit();");
        txtDebit.Enabled = false;
        txtDebit.AutoCompleteType = AutoCompleteType.Disabled;
        htcell3.Controls.Add(txtDebit);
        LstDebit.Add(txtDebit.ID);

        HtmlTableCell htcell4 = new HtmlTableCell();
        htcell4.Width = "20%";
        htcell4.VAlign = "Top";
        TextBox txtCredit = new TextBox();
        txtCredit.ID = "txtCredit" + ControlCounter.ToString();
        txtCredit.Width = Unit.Percentage(98);
        txtCredit.CssClass = "inputnumber";
        txtCredit.MaxLength = 21;
        txtCredit.ToolTip = "Credit Amount";
        txtCredit.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event);");
        txtCredit.Attributes.Add("onfocus", "return RemoveZeroOnFocusCredit(this.id);");
        txtCredit.Attributes.Add("onblur", "return SetZeroOnBlurCredit();");
        txtCredit.Text = "0.00";

        txtCredit.AutoCompleteType = AutoCompleteType.Disabled;
        htcell4.Controls.Add(txtCredit);
        LstCredit.Add(txtCredit.ID);

        HtmlTableCell htcell5 = new HtmlTableCell();
        htcell5.Width = "20%";
        htcell5.VAlign = "Top";
        TextBox txtBalance = new TextBox();
        txtBalance.ID = "txtBalance" + ControlCounter.ToString();
        txtBalance.Width = Unit.Percentage(98);
        txtBalance.CssClass = "inputnumber";
        txtBalance.Enabled = false;
        txtBalance.ToolTip = "Ledger Balance";
        htcell5.Controls.Add(txtBalance);
        LstBalance.Add(txtBalance.ID);

        htrow.Cells.Add(htcell1);
        htrow.Cells.Add(htcell2);
        htrow.Cells.Add(htcell3);
        htrow.Cells.Add(htcell4);
        htrow.Cells.Add(htcell5);
        tblVouEntry.Rows.Add(htrow);

        ViewState["LstList"] = LstList;
        ViewState["LstVouSerial"] = LstVouSerial;
        ViewState["LstLedgerName"] = LstLedgerName;
        ViewState["LstDrCr"] = LstDrCr;
        ViewState["LstDebit"] = LstDebit;
        ViewState["LstCredit"] = LstCredit;
        ViewState["LstBalance"] = LstBalance;
        hidControl.Value = ControlCounter.ToString();
        _BindLedger_ForDebitAndCredit(ddlDrCr.SelectedIndex, ControlCounter);
    }
    private void _DeleteLastRow()
    {
        int rowCount = Convert.ToInt32(hidControl.Value);
        int rowIndex = rowCount;
        int tblRowCount = Convert.ToInt32(tblVouEntry.Rows.Count);
        if (rowCount > 2)
        {
            if (rowCount == tblRowCount - 1)
            {

                LstList = (List<string>)ViewState["LstList"];
                LstLedgerName = (List<string>)ViewState["LstLedgerName"];
                LstDrCr = (List<string>)ViewState["LstDrCr"];
                LstDebit = (List<string>)ViewState["LstDebit"];
                LstCredit = (List<string>)ViewState["LstCredit"];
                LstBalance = (List<string>)ViewState["LstBalance"];
                HiddenField hidVouserial = (HiddenField)tblVouEntry.Rows[tblRowCount - 1].FindControl("hidVouserial" + rowCount);
                LstVouSerialForDelete = (List<string>)ViewState["LstVouSerialForDelete"];
                if (hidVouserial.Value != string.Empty && Convert.ToDouble(hidVouserial.Value) > 0)
                {
                    LstVouSerialForDelete.Add(hidVouserial.Value);
                }
                tblVouEntry.Rows.RemoveAt(rowCount);
                rowIndex = rowCount - 1;
                LstList.RemoveAt(rowCount - 3);
                LstLedgerName.RemoveAt(rowCount - 3);
                LstDrCr.RemoveAt(rowCount - 3);
                LstDebit.RemoveAt(rowCount - 3);
                LstCredit.RemoveAt(rowCount - 3);
                LstBalance.RemoveAt(rowCount - 3);
                ViewState["LstList"] = LstList;
                ViewState["LstLedgerName"] = LstLedgerName;
                ViewState["LstDrCr"] = LstDrCr;
                ViewState["LstDebit"] = LstDebit;
                ViewState["LstCredit"] = LstCredit;
                ViewState["LstBalance"] = LstBalance;
                ViewState["LstVouSerialForDelete"] = LstVouSerialForDelete;
                hidControl.Value = rowIndex.ToString();
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "SetZeroOnBlurDebit(); SetZeroOnBlurCredit();", true);
            }
        }
    }
    private void _BindPaymentMain(string strTranId)
    {
        if (strTranId != string.Empty)
        {
            SqlParameter[] sparam ={
                                       new SqlParameter("@TransactionId",strTranId)
                                   };
            DataTable DTMian = DU.getdatausingprocedure("USP_ACC_GetTransaction", sparam);
            if (DTMian.Rows.Count > 0)
            {
                strsql = "SELECT PREFIX,SUFFIX FROM ACC_TRANSETTINGDETAILS WHERE FINYEAR='" + DTMian.Rows[0]["FINYEAR"].ToString() + "' AND TYPEID='" + DTMian.Rows[0]["TRANTYPE"].ToString() + "'";
                DataTable DTPrefix = DU.getdata(strsql);
                if (DTPrefix.Rows.Count > 0)
                {
                    if (DTPrefix.Rows[0]["PREFIX"].ToString() != string.Empty)
                    {
                        txtPrefix.Visible = true;
                        txtPrefix.Text = DTPrefix.Rows[0]["PREFIX"].ToString();
                    }
                    if (DTPrefix.Rows[0]["SUFFIX"].ToString() != string.Empty)
                    {
                        txtSuffix.Visible = true;
                        txtSuffix.Text = DTPrefix.Rows[0]["SUFFIX"].ToString();
                    }
                    txtVouNo.Text = DTMian.Rows[0]["VOUNO"].ToString();
                    if (DTMian.Rows[0]["VOUDATE"].ToString() != string.Empty)
                    {
                        txtDate.Text = Convert.ToDateTime(DTMian.Rows[0]["VOUDATE"].ToString()).ToString("dd/MM/yyyy").Replace("01/01/1900", "");
                    }
                    txtNarration.Text = DTMian.Rows[0]["NARRATION"].ToString();
                    hidTranId.Value = DTMian.Rows[0]["TRANID"].ToString();
                    hidTranType.Value = DTMian.Rows[0]["TRANTYPE"].ToString();
                    txtChequeNo.Text = DTMian.Rows[0]["CHEQUENO"].ToString();
                }
            }
        }
    }
    private void _BindPaymentItemDetails(string strTranId)
    {
        if (strTranId != string.Empty)
        {
            SqlParameter[] sparam ={
                                       new SqlParameter("@TransactionId",strTranId)
                                   };
            DataTable DTDetails = DU.getdatausingprocedure("USP_ACC_GetTransactionItem", sparam);
            if (DTDetails.Rows.Count > 0)
            {
                if (DTDetails.Rows.Count > 2)
                {
                    for (int i = 2; i < DTDetails.Rows.Count; i++)
                    {
                        _AddNewRow();
                    }
                }
                for (int i = 0; i < DTDetails.Rows.Count; i++)
                {
                    DropDownList ddlDrCr = (DropDownList)tblVouEntry.Rows[i + 1].FindControl("ddlDrCr" + (i + 1));
                    HiddenField hidVouserial = (HiddenField)tblVouEntry.Rows[i + 1].FindControl("hidVouserial" + (i + 1));
                    DropDownList ddlLedger = (DropDownList)tblVouEntry.Rows[i + 1].FindControl("ddlLedger" + (i + 1));
                    TextBox txtDebit = (TextBox)tblVouEntry.Rows[i + 1].FindControl("txtDebit" + (i + 1));
                    TextBox txtCredit = (TextBox)tblVouEntry.Rows[i + 1].FindControl("txtCredit" + (i + 1));
                    TextBox txtBalance = (TextBox)tblVouEntry.Rows[i + 1].FindControl("txtBalance" + (i + 1));
                    hidVouserial.Value = DTDetails.Rows[i]["VOUSERIAL"].ToString();
                    if (DTDetails.Rows[i]["AMOUNT"].ToString() != string.Empty)
                    {
                        double dblAmount = Convert.ToDouble(DTDetails.Rows[i]["AMOUNT"].ToString());
                        if (dblAmount < 0)
                        {
                            ddlDrCr.SelectedIndex = 1;
                            _BindLedger_ForDebitAndCredit(ddlDrCr.SelectedIndex, i + 1);
                            ddlLedger.SelectedValue = DTDetails.Rows[i]["LID"].ToString();
                            txtBalance.Text = _GetLedgerCurrentBalance(DTDetails.Rows[i]["LID"].ToString(), Session["FINYEAR"].ToString());
                            txtDebit.Enabled = true;
                            txtDebit.Text = string.Format("{0:0.00}", Math.Abs(dblAmount)).ToString();
                            txtCredit.Text = "";
                            txtCredit.Enabled = false;

                        }
                        else
                        {
                            ddlDrCr.SelectedIndex = 0;
                            _BindLedger_ForDebitAndCredit(ddlDrCr.SelectedIndex, i + 1);
                            ddlLedger.SelectedValue = DTDetails.Rows[i]["LID"].ToString();
                            txtBalance.Text = _GetLedgerCurrentBalance(DTDetails.Rows[i]["LID"].ToString(), Session["FINYEAR"].ToString());
                            txtCredit.Enabled = true;
                            txtCredit.Text = string.Format("{0:0.00}", Math.Abs(dblAmount)).ToString();
                            txtDebit.Text = "";
                            txtDebit.Enabled = false;

                        }
                    }
                }
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "SetZeroOnBlurDebit(); SetZeroOnBlurCredit();", true);
            }
        }
    }
    private void _SaveVoucherMain(string strTranId)
    {
        try
        {
            if (strTranId != string.Empty)
            {
                string strTranDate = string.Empty;
                string strFinYear = Session["FINYEAR"].ToString();
                if (txtDate.Text != "")
                {
                    string[] str = txtDate.Text.ToString().Split('/');
                    strTranDate = str[1].ToString() + "/" + str[0].ToString() + "/" + str[2].ToString();
                }

                if (strFinYear != string.Empty)
                {

                    SqlParameter[] sparam =
                                        {
                                            new SqlParameter("@TransactionID",strTranId),
                                            new SqlParameter("@TransactionTypeID",hidTranType.Value),
                                            new SqlParameter("@TransactionNumber",txtVouNo.Text.Trim()),
                                            new SqlParameter("@FinYear",strFinYear),
                                            new SqlParameter("@ReferenceNumber",txtVouNo.Text.Trim()),
                                            new SqlParameter("@IsCashVoucher","1"),
                                            new SqlParameter("@TransactionDate",strTranDate),
                                            new SqlParameter("@Narration",txtNarration.Text.Trim()),
                                            new SqlParameter("@CreditDays","0"),
                                            new SqlParameter("@ChequeNumber",txtChequeNo.Text.Trim()),                                         
                                            new SqlParameter("@ModifiedBy",Session["Username"].ToString()),
                                            new SqlParameter("@IsCancelled","0"),
                                            new SqlParameter("@CostCentreClassID","0")
                                        };
                    int result = DU.executeprocedure("USP_ACC_UpdateTransaction", sparam);
                    if (result > 0)
                    {
                        _SaveVoucherDetials(strTranId);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void _SaveVoucherDetials(string strTranId)
    {
        try
        {
            if (strTranId != string.Empty)
            {
                for (int i = 1; i < tblVouEntry.Rows.Count; i++)
                {
                    DropDownList ddlDrCr = (DropDownList)tblVouEntry.Rows[i].FindControl("ddlDrCr" + i);
                    HiddenField hidVouserial = (HiddenField)tblVouEntry.Rows[i].FindControl("hidVouserial" + i);
                    DropDownList ddlLedger = (DropDownList)tblVouEntry.Rows[i].FindControl("ddlLedger" + i);
                    TextBox txtDebit = (TextBox)tblVouEntry.Rows[i].FindControl("txtDebit" + i);
                    TextBox txtCredit = (TextBox)tblVouEntry.Rows[i].FindControl("txtCredit" + i);
                    if (hidVouserial.Value == "0")
                    {

                        if (ddlDrCr.SelectedIndex == 1)
                        {
                            if (Convert.ToDouble(txtDebit.Text) > 0)
                            {
                                double DebitAmt = 0 - Convert.ToDouble(txtDebit.Text.ToString());
                                SqlParameter[] sparam ={
                                                    new SqlParameter("@TransactionId",strTranId),
                                                    new SqlParameter("@LedgerId",ddlLedger.SelectedValue),
                                                    new SqlParameter("@Amount",DebitAmt.ToString()),
                                                    new SqlParameter("@IsReconciled","0"),
                                                };
                                DU.executeprocedure("USP_ACC_SaveTransactionDetails", sparam);
                            }
                        }
                        else if (ddlDrCr.SelectedIndex == 0)
                        {
                            if (Convert.ToDouble(txtCredit.Text) > 0)
                            {
                                SqlParameter[] sparam ={
                                                    new SqlParameter("@TransactionId",strTranId),
                                                    new SqlParameter("@LedgerId",ddlLedger.SelectedValue),
                                                    new SqlParameter("@Amount",txtCredit.Text.Trim()),
                                                    new SqlParameter("@IsReconciled","0"),
                                                };
                                DU.executeprocedure("USP_ACC_SaveTransactionDetails", sparam);
                            }
                        }
                    }
                    else if (hidVouserial.Value != "0")
                    {
                        if (ddlDrCr.SelectedIndex == 1)
                        {
                            if (Convert.ToDouble(txtDebit.Text) > 0)
                            {
                                double DebitAmt = 0 - Convert.ToDouble(txtDebit.Text.ToString());
                                SqlParameter[] sparam ={
                                                    new SqlParameter("@TransactionSerial",hidVouserial.Value),
                                                    new SqlParameter("@TransactionId",strTranId),
                                                    new SqlParameter("@LedgerId",ddlLedger.SelectedValue),
                                                    new SqlParameter("@Amount",DebitAmt.ToString()),
                                                    new SqlParameter("@IsReconciled","0"),
                                                };
                                DU.executeprocedure("USP_ACC_UpdateTransactionDetails", sparam);
                            }
                        }
                        else if (ddlDrCr.SelectedIndex == 0)
                        {
                            if (Convert.ToDouble(txtCredit.Text) > 0)
                            {
                                SqlParameter[] sparam ={
                                                    new SqlParameter("@TransactionSerial",hidVouserial.Value),
                                                    new SqlParameter("@TransactionId",strTranId),
                                                    new SqlParameter("@LedgerId",ddlLedger.SelectedValue),
                                                    new SqlParameter("@Amount",txtCredit.Text.Trim()),
                                                    new SqlParameter("@IsReconciled","0"),
                                                    };
                                DU.executeprocedure("USP_ACC_UpdateTransactionDetails", sparam);
                            }
                        }
                    }
                }
                LstVouSerialForDelete = (List<string>)ViewState["LstVouSerialForDelete"];
                if (LstVouSerialForDelete != null)
                {
                    int intDeleteCount = LstVouSerialForDelete.Count;
                    for (int j = 0; j < intDeleteCount; j++)
                    {
                        strsql = "DELETE FROM ACC_VOUDETAILS WHERE VOUSERIAL='" + LstVouSerialForDelete[j].ToString() + "' AND TRANID='" + strTranId + "'";
                        DU.executesql(strsql);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void _BindLedger_ForDebitAndCredit(int CreditOrDebit, int RowNo)
    {
        SqlParameter[] sparam =
                                {
                                    new SqlParameter("@CreditOrDebit",CreditOrDebit.ToString())
                                };
        DataTable DT = DU.getdatausingprocedure("USP_ACC_GetLedgerForReceiptTransaction", sparam);
        if (DT.Rows.Count > 0)
        {
            DropDownList ddlLedger = (DropDownList)tblVouEntry.Rows[RowNo].FindControl("ddlLedger" + RowNo);
            ddlLedger.DataSource = DT;
            ddlLedger.DataTextField = "LNAME";
            ddlLedger.DataValueField = "LID";
            ddlLedger.DataBind();
            ddlLedger.Items.Insert(0, new ListItem("", "-1"));
        }

    }
    private string _GetLedgerCurrentBalance(string strLID, string strFinYear)
    {
        string strCurrentBalance = "0.00";
        if (strLID != string.Empty && strLID != "-1")
        {
            SqlParameter[] sparam =
                                    {
                                        new SqlParameter("@Lid",strLID),
                                        new SqlParameter("@Finyear",strFinYear)
                                    };
            DataTable DT = DU.getdatausingprocedure("USP_ACC_GetLedgerCurrentBalance", sparam);
            if (DT.Rows.Count > 0)
            {
                strCurrentBalance = Convert.ToDouble(DT.Rows[0]["ClosingBal"].ToString()).ToString("N2") + " " + DT.Rows[0]["DebitCredit"].ToString();
            }
        }
        return strCurrentBalance;
    }
    protected void ddlLedger_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        string ddlId = ddl.ID.ToString().Substring(9);
        int rowNo = Convert.ToInt32(ddlId);
        TextBox txtBalance = (TextBox)tblVouEntry.Rows[rowNo].FindControl("txtBalance" + rowNo);
        txtBalance.Text = _GetLedgerCurrentBalance(ddl.SelectedValue, Session["FINYEAR"].ToString());
    }
    protected void ddlDrCr_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        string ddlid = ddl.ID.ToString().Substring(7);
        int rowIndex = Convert.ToInt32(ddlid.ToString());
        TextBox txtDebit = (TextBox)tblVouEntry.Rows[rowIndex].FindControl("txtDebit" + rowIndex);
        TextBox txtCredit = (TextBox)tblVouEntry.Rows[rowIndex].FindControl("txtCredit" + rowIndex);
        if (ddl.SelectedIndex == 0)
        {
            _BindLedger_ForDebitAndCredit(ddl.SelectedIndex, rowIndex);
            txtDebit.Text = "";
            txtDebit.Enabled = false;
            txtCredit.Enabled = true;
            txtCredit.Text = "0.00";
        }
        else if (ddl.SelectedIndex == 1)
        {
            _BindLedger_ForDebitAndCredit(ddl.SelectedIndex, rowIndex);
            txtDebit.Enabled = true;
            txtDebit.Text = "0.00";
            txtCredit.Text = "";
            txtCredit.Enabled = false;
        }
        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "SetZeroOnBlurDebit(); SetZeroOnBlurCredit();", true);
    }
    protected void btnAddRow_Click(object sender, EventArgs e)
    {
        _AddNewRow();
    }
    protected void btnDeleteRow_Click(object sender, EventArgs e)
    {
        _DeleteLastRow();
    }
    protected void btnClose_Click(object sender, EventArgs e)
    {
        Response.Redirect("../VoucherDisplay/Receipt_view.aspx");
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            using (TransactionScope scope = new TransactionScope())
            {
                _SaveVoucherMain(hidTranId.Value);
                scope.Complete();
            }
            Response.Redirect("../VoucherDisplay/Receipt_view.aspx");
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }
    }

}
